﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;

namespace Designit.Umbraco.Dashboard.MultiFileUpload.usercontrols.Designit.Dashboard.MultiFileUpload
{
    public partial class MultiFileUpload : System.Web.UI.UserControl
    {
        #region Private Properties

        string folder = "";

        string fileExt = "";

        string fileDesc = "";

        string sizeLimit = "2620000";

        Dictionary<string, string> scriptData;

        bool auto = true;

        string onSelect = "function(event, queueID, fileObj) { }";

        string onComplete = "function(event, queueID, fileObj) { }";

        string onError = "function(event, queueID, fileObj, errorObj) { }";

        bool showSizeLimit = false;

        string sizeLimitClass = "";

        bool showPreview = false;

        string previewImage = "";
       
        bool multi = true;

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            var head = Page.Header;
            var link = new HtmlLink();
            link.Attributes.Add("href", "/css/Designit.Dashboard.MultiFileUpload/uploadify.css");
            link.Attributes.Add("type", "text/css");
            link.Attributes.Add("rel", "stylesheet");
            head.Controls.Add(link);

            onSelect = "function(event, queueID, fileObj) { $(\"#buttons\").toggle(); }";
            btnClear.OnClientClick = string.Format("javascript:$('#{0}').fileUploadClearQueue();$(\"#buttons\").toggle();return false;", fileSelector.ClientID);
            btnUpload.OnClientClick = string.Format("javascript:$('#{0}').fileUploadStart();$(\"#buttons\").toggle();return false;", fileSelector.ClientID);

            RegisterJavaScript();
        }

        public void Reload()
        {
            ScriptManager.RegisterStartupScript(this.Page, typeof(Page), "LoadVars" + ClientID, "LoadUploadVariables_" + ClientID + "();", true);
        }

        public string GenerateScriptData()
        {
            string data = "";

            if(scriptData != null)
                foreach (KeyValuePair<string, string> kvp in scriptData)
                    data += ",'" + kvp.Key + "' : '" + kvp.Value + "'";

            data += ",'ClientID':'" + this.ClientID + "'";

            return "{ " + data.Substring(1) + " }";
        }

        private void RegisterJavaScript()
        {
            umbraco.library.AddJquery();
            if(!Page.ClientScript.IsClientScriptIncludeRegistered("uploadify"))
                Page.ClientScript.RegisterClientScriptInclude("uploadify", "/scripts/Designit.Dashboard.MultiFileUpload/jquery.uploadify.js");

            if (!Page.ClientScript.IsStartupScriptRegistered("uploadifyInit"))
            {
                var sb = new StringBuilder();
                sb.AppendLine(@"<script type=""text/javascript"">");
                sb.AppendLine("$(window).load(");
                sb.AppendLine("function() {");
                sb.AppendLine(string.Format("  LoadUploadVariables_{0}();", ClientID));
                sb.AppendLine("}");
                sb.AppendLine(");");

                sb.AppendLine(string.Format("function LoadUploadVariables_{0}()", ClientID));
                sb.AppendLine("{");
                sb.AppendLine(string.Format("$(\"#{0}\").fileUpload({{", fileSelector.ClientID));
                sb.AppendLine("'uploader': '/Flash/Designit.Dashboard.MultiFileUpload/uploader.swf',");
                sb.AppendLine("'buttonImg': '/images/Designit.Dashboard.MultiFileUpload/browse.png',");
                sb.AppendLine("'cancelImg': '/images/Designit.Dashboard.MultiFileUpload/cancel.png',");
                sb.AppendLine(string.Format("'folder': '{0}',", folder));
                sb.AppendLine("'script': '/usercontrols/Designit.Dashboard.MultiFileUpload/Upload.ashx',");
                //'fileExt': '<%=FileExt %>',
                //'fileDesc': '<%=FileDesc %>',
                //'sizeLimit': '<%=SizeLimit %>',
                sb.AppendLine(string.Format("'scriptData': {0},", GenerateScriptData()));
                sb.AppendLine("'auto': false,");
                sb.AppendLine("'width': 150,");
                //'height': <%=height %>,
                sb.AppendLine(string.Format("'onSelect': {0},", onSelect));
                //'onComplete': <%=OnComplete %>,
                //'onError': <%=OnUploadError %>,
                sb.AppendLine("'multi': true");
                sb.AppendLine("});");
                sb.AppendLine("}");
                sb.AppendLine("</script>");

                Page.ClientScript.RegisterStartupScript(GetType(), "uploadifyInit", sb.ToString(), false);
            }

        }
    }
}